package com.caowei.weicimpress.weicimpress.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.caowei.weicimpress.weicimpress.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by caoweiPc on 2014/7/17.
 */
public class DropdownListWidget extends DialogWidget {

    protected ActionbarDropdownListView mListView;
    private AdapterView.OnItemClickListener onItemClickListener ;
    private View.OnKeyListener  onKeyListener;
    private View preSelected;
    public DropdownListWidget(Context context) {
        super(context);
        setContentView(R.layout.dropdownlist);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        final View v = getContentView();
        mListView= (ActionbarDropdownListView) v.findViewById(R.id.list);
    }

    @Override
    protected void afterShow() {
      //空方法
    }

    public DropdownListWidget(final Context context,
                              final AdapterView.OnItemClickListener onItemClickListener) {
        this(context);
        this.onItemClickListener=new AdapterView.OnItemClickListener() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                view.setBackgroundResource(R.drawable.dropmenuitemselectbg);
                TextView textView= (TextView) view.findViewById(R.id.text);
                textView.setTextColor(context.getResources().getColor(R.color.green_deep));
                if(preSelected!=null&&preSelected!=view){
                if(Build.VERSION_CODES.JELLY_BEAN>Build.VERSION.SDK_INT){
                        preSelected.setBackgroundDrawable(null);
                    }else{
                        preSelected.setBackground(null);
                    }
                    TextView textView1= (TextView) preSelected.findViewById(R.id.text);
                    textView1.setTextColor(context.getResources().getColor(R.color.gray_deep));
                }

                preSelected=view;
                ActionItem actionItem=getActionItem(position);
                if(actionItem.getCallback()!=null){
                    actionItem.getCallback().callback();
                }
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(parent,view,position,id);
                }
            }
        };

        this.onKeyListener=new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                int action=event.getAction();
                if((KeyEvent.KEYCODE_BACK==keyCode||KeyEvent.KEYCODE_MENU==keyCode)&&event.getRepeatCount()==0){
                    if(DropdownListWidget.this.isShowing()){
                        DropdownListWidget.this.dismiss();
                    }
                }
                return true;
            }
        };
    }



    @Override
    protected void populateActionItems(final List<ActionItem> actionItems) {
        BaseAdapter adapter=new BaseAdapter() {
            @Override
            public int getCount() {
                return actionItems.size();
            }

            @Override
            public ActionItem getItem(int position) {
                return actionItems.get(position);
            }

            @Override
            public long getItemId(int position) {
                return getItem(position).hashCode();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v=convertView;
                ViewHolder viewHolder=null;
                if(v==null){
                    v= LayoutInflater.from(getContext()).inflate(R.layout.dropdownitem,mListView,false);
                    viewHolder=new ViewHolder(v);
                    v.setTag(viewHolder);
                }else{
                    viewHolder= (ViewHolder) convertView.getTag();
                }
                ActionItem actionItem=getActionItem(position);
                viewHolder.imageView.setImageResource(actionItem.getIcoId());
                viewHolder.textView.setText(actionItem.getMenuName());
                return v;
            }
        };
        mListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        setMisDirty(false);
        mListView.setOnItemClickListener(onItemClickListener);
        mListView.setOnKeyListener(this.onKeyListener);
    }

     static class ViewHolder{
         @InjectView(R.id.text)
        TextView textView;
         @InjectView(R.id.ico)
        ImageView imageView;

        public ViewHolder(View v){
            ButterKnife.inject(this,v);
        }
    }
    @Override
    protected void onMeasureAndLayout(Rect anchorRect, View contentView) {
         contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
         contentView.measure(View.MeasureSpec.makeMeasureSpec(getWidth()-4, View.MeasureSpec.EXACTLY),
                ViewGroup.LayoutParams.WRAP_CONTENT);
        int rootHeight = contentView.getMeasuredHeight();

        int dyTop = anchorRect.top;
        int dyBottom = getScreenHeight() - anchorRect.bottom;
        boolean onTop = (dyTop > dyBottom);
        int popupY = (onTop) ? anchorRect.top - rootHeight  : anchorRect.bottom ;

        int dxLeft = getScreenWidth()-anchorRect.left;
        boolean onLeft= dxLeft>anchorRect.right;
        int popupX=(onLeft)?anchorRect.left:(anchorRect.right-getWidth());

        setWidgetSpecs(popupY,onTop,popupX,onLeft);
    }

    public  static interface Callback{
        public  void callback();
    }

    public static interface OnListViewFinishChildren{
        public void onFinish();
    };
}
